package com.scope.customViews;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Camera;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

/**
* Created by Samrith on 11/4/13.
*/
public class ImageRotatedView extends ImageButton {
    public ImageRotatedView(Context context) {
        super(context);
        setWillNotDraw(false);

        mScreenView = new ImageView(context);
        mScreenView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        addView(mScreenView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

        mScreenAlpha = 255;
        mCamera = new Camera();
    }

    @Override
    public void setImageDrawable(Drawable image) {
        super.setImageDrawable(image);

        if (mScreenAlpha < 255) {
            int screenColor = Color.argb(mScreenAlpha, 110, 110, 110);

            if (mScreenView != null) {
                Bitmap bmp = Bitmap.createBitmap(
                        image.getIntrinsicWidth(),
                        image.getIntrinsicHeight(),
                        Bitmap.Config.ARGB_8888
                );
                bmp.eraseColor(screenColor);
                mScreenView.setImageBitmap(bmp);
            }
        }
    }

    private int mScreenAlpha;
    public void setScreenAlpha(int alpha) {
        mScreenAlpha = alpha;
    }

    private ImageView mScreenView;
    private Camera mCamera;
    private float mAngleX;
    private float mAngleY;
    private float mAngleZ;
    public void setAngle(float angleX, float angleY, float angleZ) {
        mAngleX = angleX;
        mAngleY = angleY;
        mAngleZ = angleZ;
        invalidate();
    }

    private final static float TRNSLT_COEFF = 0.4f;
    @Override
    protected void onDraw(Canvas canvas) {
        float angle = Math.max(Math.abs(mAngleX), Math.abs(mAngleY));
        final int floor = (int)(angle / 90f);
        if (floor % 2 == 0) {
            angle = angle - (floor * 90f);
        } else {
            angle = ((floor + 1) * 90f) - angle;
        }
        Log.i(this.toString(), "angle = " + angle);
        final float trnsltCoeff = TRNSLT_COEFF * angle + 1f;

        final Camera camera = mCamera;
        camera.save();
        final float cameraLocZ = camera.getLocationZ();

        camera.rotateX(mAngleX);
        camera.rotateY(mAngleY);
        camera.rotateZ(mAngleZ);
        camera.translate(0, 0, (-1f) * trnsltCoeff * cameraLocZ);
        Log.i(this.toString(), "Camera: " + camera.getLocationX() + ", "
                + camera.getLocationY() + ", " + camera.getLocationZ());

        Matrix matrix = new Matrix();
        camera.getMatrix(matrix);

        final float centerX = getWidth() / 2.0f;
        final float centerY = getHeight() / 2.0f;
        matrix.preTranslate(-centerX, -centerY);
        matrix.postTranslate(centerX, centerY);
        camera.restore();

        canvas.concat(matrix);
        super.onDraw(canvas);
    }

}
